package com.hongyang.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hongyang.dto.BabyDTO;
import com.hongyang.entity.Baby;
import com.hongyang.entity.Midwife;
import com.hongyang.mapper.BabyMapper;
import com.hongyang.mapper.MidwifeMapper;
import com.hongyang.service.BabyService;
import com.hongyang.util.MybatisUtil;
import com.hongyang.vo.BabyVO;
import com.hongyang.vo.BootstrapTableVO;
import com.hongyang.vo.ResultEnum;
import com.hongyang.vo.ResultVO;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class BabyServiceImpl implements BabyService {
    //新增婴儿信息
    @Override
    public boolean insert(Baby record) {
        int count=0;
        try (SqlSession session= MybatisUtil.getSqlSessionFactory().openSession()){
            BabyMapper mapper = session.getMapper(BabyMapper.class);
            count=mapper.insert(record);
            session.commit();
        }
        return count>0;
    }

    //婴儿分页查询
    @Override
    public BootstrapTableVO search(BabyDTO babyDTO) {
        BootstrapTableVO tableVO=new BootstrapTableVO();
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            //婴儿mapper
            BabyMapper babyMapper = session.getMapper(BabyMapper.class);
            //接生员mapper
            MidwifeMapper midwifeMapper=session.getMapper(MidwifeMapper.class);

            //设置分页
            PageHelper.startPage(babyDTO.getPageNumber(), babyDTO.getPageSize());
            //查询数据
            List<Baby> babyList = babyMapper.search(babyDTO);
            //得到总记录数
            PageInfo<Baby>   pg=new PageInfo<>(babyList);
            tableVO.setTotal(pg.getTotal());

            //循环每一笔数据  组装的对象
            List<BabyVO> list=new ArrayList<>();
            for (Baby baby : babyList) {
                // 把Baby 转换成 BabyVO
                BabyVO babyVO=new BabyVO();
                // 把 b 里面的数据 全部  copy 到  babyVO
                BeanUtils.copyProperties(babyVO,baby);
                //查询接员的姓名  接生员姓名
                Midwife midwife = midwifeMapper.selectByPrimaryKey(baby.getMidwifeId());
                babyVO.setMidwifeName(midwife.getMidwifeName());
                list.add(babyVO);
            }
            tableVO.setRows(list);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return tableVO;
    }

    @Override
    public ResultVO<String> deleteByPrimaryKey(String id) {
        int count=0;
        try(SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            BabyMapper mapper=session.getMapper(BabyMapper.class);
            count=mapper.deleteByPrimaryKey(id);
            //提交事务
            session.commit();
        }
        ResultVO<String> vo=null;
        if(count>0){
            vo=ResultVO.success("删除成功");
        }else{
            vo=ResultVO.failed(ResultEnum.DB_DELETE_FAILED);
        }

        return vo;
    }

    @Override
    public boolean updateByPrimaryKey(Baby record) {
        int count=0;
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            BabyMapper mapper = session.getMapper(BabyMapper.class);
            count = mapper.updateByPrimaryKey(record);
            session.commit();
        }
        return count>0;
    }

    @Override
    public Baby selectByPrimaryKey(String babyId) {
        Baby baby=null;
        try (SqlSession session=MybatisUtil.getSqlSessionFactory().openSession()){
            BabyMapper mapper = session.getMapper(BabyMapper.class);
            baby=mapper.selectByPrimaryKey(babyId);
        }
        return baby;
    }


}
